home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9555 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: news.clark.net!not-for-mail
  2. From: gusty@clark.net (Harlan Messinger)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: copy vs. assignment
  5. Date: 2 Mar 1996 17:23:04 GMT
  6. Organization: Clark Internet Services, Inc., Ellicott City, MD USA
  7. Message-ID: <4ha05o$7u8@clarknet.clark.net>
  8. References: <4h7va1$1ev@mother.usf.edu>
  9. NNTP-Posting-Host: explorer.clark.net
  10. Mime-Version: 1.0
  11. Content-Type: TEXT/PLAIN; charset=ISO-8859-1
  12. Content-Transfer-Encoding: 8bit
  13. X-Newsreader: TIN [UNIX 1.3 950726BETA PL0]
  14.  
  15. Timothy Miller (millert@boheme.csee.usf.edu) wrote:
  16. : Well, it seems that overloading the = operator for assigning one instance 
  17. : of a class to one of its own kind is futile, because no matter what, it 
  18. : uses the copy constructor to copy the class.
  19.  
  20. This is not true.
  21.  
  22. : My problem is that I need to do one thing if an object is initialized by 
  23. : one of its own kind, like this;
  24. : Matrix p;
  25. : // stuff done to p;
  26. : Matrix q = p;  // <- right here
  27. : And I need to do something else if an already existing object is being 
  28. : assigned to like this:
  29. : Matrix p, q;
  30. : // stuff done to p
  31. : q = p;   // <- this should call a different class member
  32. : No matter what I do, it seems that q gets initialized by the 
  33. : constructor, regardless of whether or not it's already been created, even 
  34. : if I overload the = operator.
  35.  
  36. Then you must be doing something wrong. Even if you don't define 
  37. operator= at all, the system will use the default operator= (memberwise 
  38. copy) rather than your copy constructor.
  39.  
  40. One possibility: perhaps you haven't set up the overload definition 
  41. correctly. For example, if you have
  42.  
  43.     Matrix &operator =(Matrix M) {
  44.         ...
  45.     }
  46.  
  47. then M, would be a "value" parameter. This means that M would be a newly
  48. constructed copy, local to your function and created using the copy
  49. constructor, of the matrix that was on the right side of the equals sign
  50. in the assignment statement. You would then be assigning from this copy,
  51. the result of which may look like the target matrix was itself set by the
  52. copy constructor. 
  53.  
  54. If this is the problem, the solution is to define the overload as
  55.  
  56.     Matrix &operator =(const Matrix& M) {
  57.         ...
  58.     }
  59.  
  60. This makes M a "reference" parameter, which means that the function goes 
  61. directly to the argument in the function call rather than making a copy 
  62. of it. This keeps the copy constructor from being invoked, and eliminates 
  63. the nuisance intermediate Matrix object created by a call-by-value.
  64.